package com.zjxny.jobs;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.zjxny.service.TrafficService;
import com.zjxny.vo.FixPhotoVO;
import com.zjxny.vo.TrafficDetailVO;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import xyz.erupt.core.annotation.EruptHandlerNaming;
import xyz.erupt.job.handler.EruptJobHandler;

@Slf4j
@Service
@EruptHandlerNaming("修复照片")
public class FixTrafficPhotoJob implements EruptJobHandler{
	@Resource
	JdbcTemplate jdbcTemplate;
	
	@Resource
	TrafficService trafficService;
	
	@Override
	public String exec(String code, String param) {
		log.info("FixTrafficPhotoJob.exec");
		
//		String sql = "SELECT t1.id trafficId,t1.hphm,t1.photos,t1.xh trafficXh,t2.xh,t2.is_operation isOperation,t2.is_not_operation isNotOperation,t2.is_biz isBiz,t3.cookies,t3.domain from traffic_info t1 left join car_info t2 on t1.car_info_id=t2.id left join account_info t3 on t3.id=t2.account_info_id WHERE t1.photos like '[%'";
		
		//粤BAL3881
//		List<Map<String,Object>> lists = jdbcTemplate.queryForList(sql);
		int batchSize = 50; // 每次查询的数据量
        int offset = 0;
        List<FixPhotoVO> dataList = null;

        do {
        	log.info("FixTrafficPhotoJob.offset={}",offset);
        	String sql = "SELECT t1.id trafficId,t1.hphm,t1.photos,t1.xh trafficXh,t1.cjjg,t2.xh,t2.is_operation isOperation,t2.is_not_operation isNotOperation,t2.is_biz isBiz,t3.cookies,t3.domain from traffic_info t1 left join car_info t2 on t1.car_info_id=t2.id left join account_info t3 on t3.id=t2.account_info_id WHERE t3.is_valid=1 AND t1.photos like '[%' LIMIT ? OFFSET ?";
//        	sql = "SELECT t1.id trafficId,t1.hphm,t1.photos,t1.xh trafficXh,t1.cjjg,t2.xh,t2.is_operation isOperation,t2.is_not_operation isNotOperation,t2.is_biz isBiz,t3.cookies,t3.domain from traffic_info t1 left join car_info t2 on t1.car_info_id=t2.id left join account_info t3 on t3.id=t2.account_info_id WHERE t1.id>666 and t3.id=2 and t3.is_valid=1 AND t1.photos like '[%' ORDER BY t1.id asc LIMIT ? OFFSET ?";
            dataList = jdbcTemplate.query(sql, new Object[]{batchSize, offset}, new DataMapper());
            for (FixPhotoVO data : dataList) {
            	//更新数据
            	TrafficDetailVO trafficDetailVO = null;
            	if(data.getIsOperation()) {
            		trafficDetailVO = trafficService.getOperationTrafficDetailOrigin(data.getCookies(), data.getDomain(), data.getHphm(), data.getTrafficXh(), data.getCjjg());
            	} else if(data.getIsBiz()) {
            		trafficDetailVO = trafficService.getBizTrafficDetailOrigin(data.getCookies(), data.getDomain(), data.getHphm(), data.getXh(), data.getTrafficXh());
            	} else {
            		System.err.println("--here---");
            		trafficDetailVO=trafficService.getTrafficDetailOrigin(data.getCookies(), data.getDomain(), data.getHphm(), data.getTrafficXh());
            	}
            	
            	if(null!=trafficDetailVO) {
            		if(StrUtil.isNotBlank(trafficDetailVO.getPhotos())) {
            			String photos = trafficService.downloadPhotos(data.getTrafficXh(), trafficDetailVO.getPhotos());
            			String usqlOne =String.format("update traffic_info set photos='%s' where id=%d",photos,data.getTrafficId());
            			jdbcTemplate.update(usqlOne);
            			
            			String usqlTwo =String.format("update traffic_detail set photos='%s' where xh='%s'",photos,data.getTrafficXh());
            			jdbcTemplate.update(usqlTwo);
            		} else {
            			log.warn("fixphoto.fail hphm={},trafficXh={},photos={}",data.getHphm(),data.getTrafficXh(),data.getPhotos());
            			String usqlOne =String.format("update traffic_info set photos=null where id=%d",data.getTrafficId());
            			jdbcTemplate.update(usqlOne);
            			
            			String usqlTwo =String.format("update traffic_detail set photos=null where xh='%s'",data.getTrafficXh());
            			jdbcTemplate.update(usqlTwo);
            		}
            	}
            }
            offset += batchSize;
        } while (dataList != null && !dataList.isEmpty());
        
		return null;
	}
	
	private static class DataMapper implements RowMapper<FixPhotoVO> {
        @Override
        public FixPhotoVO mapRow(ResultSet resultSet, int rowNum) throws SQLException {
            // 将结果集映射到 Data 对象
        	FixPhotoVO data = new FixPhotoVO();

        	data.setTrafficId(resultSet.getLong("trafficId"));
            data.setDomain(resultSet.getString("domain"));
            data.setCookies(resultSet.getString("cookies"));
            
            data.setIsOperation(resultSet.getBoolean("isOperation"));
            data.setIsNotOperation(resultSet.getBoolean("isNotOperation"));
            data.setIsBiz(resultSet.getBoolean("isBiz"));
            
            data.setHphm(resultSet.getString("hphm"));
            data.setTrafficXh(resultSet.getString("trafficXh"));
            data.setXh(resultSet.getString("xh"));
            data.setPhotos(resultSet.getString("photos"));
            
            data.setCjjg(resultSet.getString("cjjg"));
            
            return data;
        }
    }
}
